package com.example.lists.LazyListState

import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.*
import androidx.compose.material.R
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun StickyHeadersSample(
    listState: LazyListState,
    scope: CoroutineScope
) {
    val sections = listOf("贡献者", "开发者")

    Column {
        TopAppBar(
            title = { Text(text = "Compose Museum 贡献者") },
            actions = {
                IconButton(
                    onClick = { /*TODO*/ }
                ) {
                    Icon(Icons.Filled.Search, contentDescription = null)
                }

                IconButton(
                    onClick = {
                        scope.launch {
                            listState.animateScrollToItem(0) //滚动到第一个项目的动画
                                                                    //scrollToItem() 滚动到第一个项目
                        }
                    }
                ) {
                    Icon(
                        painter = painterResource(id = com.example.lists.R.drawable.top),
                        contentDescription = null,
                        modifier = Modifier.size(28.dp),
                        tint = Color.DarkGray
                    )
                }
            },
            backgroundColor = Color.Green
        )
        LazyColumn(
            state = listState
        ) {
            sections.forEachIndexed { index, section ->
                stickyHeader {
                    Text(
                        text = section,
                        modifier = Modifier
                            .fillMaxWidth()
                            .background(Color.LightGray)
                            .padding(horizontal = 16.dp, vertical = 8.dp),
                        fontWeight = FontWeight.W700,
                        color = Color.Blue
                    )
                }

                when (index) {
                    0 -> item { Contributor() }
                    1 -> item { Developerment() }
                }
            }
        }
    }

}

//定义MemberItem
@Composable
fun MemberItem(
    imageID: Int,
    text: String,
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 10.dp, vertical = 5.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Surface(
            shape = CircleShape,
            modifier = Modifier.size(40.dp)
        ) {
            Image(painter = painterResource(imageID), contentDescription = null)
        }
        Spacer(Modifier.padding(horizontal = 8.dp))
        Text(text = text, style = MaterialTheme.typography.body1, fontWeight = FontWeight.W500)
        Box(
            modifier = Modifier.fillMaxWidth(),
            contentAlignment = Alignment.CenterEnd
        ) {
            IconButton(
                onClick = { /*TODO*/ }
            ) {
                Icon(Icons.Filled.Email, null)
            }
        }
    }
}

//贡献者
@Composable
fun Contributor() {
    Column {
        MemberItem(imageID = com.example.lists.R.drawable.m1, text = "怪喵666")
        MemberItem(imageID = com.example.lists.R.drawable.m2, text = "malaxiangguo")
        MemberItem(imageID = com.example.lists.R.drawable.m3, text = "马丁龙猪")
        MemberItem(imageID = com.example.lists.R.drawable.m4, text = "抹去")
        MemberItem(imageID = com.example.lists.R.drawable.m5, text = "抹布")
    }
}

@Composable
fun Developerment() {
    for (index in 0..20) MemberItem(imageID = com.example.lists.R.drawable.m6, text = "Developer")
}